package com.apotik.legenda.dao;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import org.hibernate.Criteria;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.apotik.legenda.model.Product;
import com.apotik.legenda.util.HibernateUtil;

public class ProductDao {

	public ProductDao() {
		// TODO Auto-generated constructor stub
	}
	
	public boolean saveOrUpdate(Product product){
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			//session.save(product);
			session.saveOrUpdate(product);
			session.getTransaction().commit();
			session.close();
			return true;
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
			return false;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Product> listByActive(boolean isActive){
		List<Product> list = new ArrayList<>();
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			
			Criteria cr = session.createCriteria(Product.class);
			cr.add(Restrictions.eq("isActive", isActive));
			list = cr.list();
			
			session.close();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Product> listByAnyCriteria(boolean isActive, String searchValue, int first, int max){
		List<Product> list = new ArrayList<>();
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			
			Criteria cr = session.createCriteria(Product.class);
			cr.add(Restrictions.eq("isActive", isActive));
			
			if(!searchValue.trim().equalsIgnoreCase("")){
				Criterion _code = Restrictions.ilike("productCode", "%"+searchValue+"%");
				Criterion _name = Restrictions.ilike("productName", "%"+searchValue+"%");
				LogicalExpression orExp = Restrictions.or(_code, _name);
				cr.add(orExp);
			}
			
			cr.setFirstResult(first);
			cr.setMaxResults(max);
			cr.addOrder(Order.asc("productId"));
			list = cr.list();
			
			session.close();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Product> loadAll(){
		List<Product> list = new ArrayList<>();
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			
			Criteria cr = session.createCriteria(Product.class);
			list = cr.list();
			
			/*String sql = "SELECT * FROM apt_product";
			SQLQuery query = session.createSQLQuery(sql);
			query.addEntity(Product.class);
			list = query.list();*/
			
			session.close();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Product> listByCodeOrName(String param){
		List<Product> list = new ArrayList<>();
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			
			Criteria cr = session.createCriteria(Product.class);
			
			//ilike for case-sensitif
			Criterion _code = Restrictions.ilike("productCode", "%"+param+"%");
			Criterion _name = Restrictions.ilike("productName", "%"+param+"%");
			
			LogicalExpression orExp = Restrictions.or(_code, _name);
			cr.add(orExp);
			
			list = cr.list();
			
			session.close();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
		return list;
	}
	
	@SuppressWarnings({ "rawtypes" })
	public int size(boolean activeCount, boolean activeValue, String searchValue){
		int result = 0;
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria cr = session.createCriteria(Product.class);
			
			if(activeCount){
				cr.add(Restrictions.eq("isActive", activeValue));
			}
			
			if(!searchValue.trim().equalsIgnoreCase("")){
				Criterion _code = Restrictions.ilike("productCode", "%"+searchValue+"%");
				Criterion _name = Restrictions.ilike("productName", "%"+searchValue+"%");
				LogicalExpression orExp = Restrictions.or(_code, _name);
				cr.add(orExp);
			}
			
			cr.setProjection(Projections.rowCount());
			
			List list = cr.list();
			
			if(list!=null){
				result = (int) list.get(0);
			}
			
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
		return result;
	}
	
}
